<?php

namespace App\Http\Controllers\CRM;

use App\Enums\UserType;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\GoodsStockChangeRequest;
use App\Http\Requests\CRM\GoodsStockRequest;
use App\Http\Requests\CRM\GoodsStockResetRequest;
use App\Http\Resources\CRM\GoodsResource;
use App\Http\Resources\CRM\GoodsStockLogResource;
use App\Models\Goods;
use App\Models\GoodsStoreStock;
use App\Models\GoodsStoreStockLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class StockController extends Controller
{
    public function add(Goods $goods, GoodsStockRequest $request)
    {
        $user = Auth::user();
        if ($user->type != UserType::System->value && $user->store_id != $request->store_id) {
            return ApiResponseHelper::json([], 400, '无权限操作该门店库存');
        }

        try {
            GoodsStoreStock::addStock($request->store_id, $goods->id, $request->count);
        } catch (\Exception $e) {
            return ApiResponseHelper::json([], 400, $e->getMessage()."无法扣减库存");
        }

        $goods = Goods::with(['stocks' => function ($query) use($request) {
            $query->where('store_id', $request->store_id);
        }])->find($goods->id);

        return new GoodsResource($goods);
    }

    //库存调拨
    public function change(Goods $goods, GoodsStockChangeRequest $request)
    {
        $user = Auth::user();
        if (($user->role->type ?? null) != UserType::System->value && $user->store_id != $request->store_id) {
            return ApiResponseHelper::json([], 400, '无权限操作该门店库存');
        }

        DB::transaction(function () use ($goods, $request) {
            GoodsStoreStock::addStock($request->store_id, $goods->id, $request->count * -1);
            GoodsStoreStock::addStock($request->to_store_id, $goods->id, $request->count);
        });

        $goods = Goods::with(['stocks' => function ($query) use($request) {
            $query->where('store_id', $request->store_id);
        }])->find($goods->id);

        return new GoodsResource($goods);
    }

    public function reset(Goods $goods, GoodsStockResetRequest $request)
    {
        $user = Auth::user();
        if (($user->role->type ?? null) != UserType::System->value && $user->store_id != $request->store_id) {
            return ApiResponseHelper::json([], 400, '无权限操作该门店库存');
        }

        $stock = $goods->stocks()->where([
            'store_id' => $request->store_id,
        ])->first();

        if (empty($stock))
            return ApiResponseHelper::json([], 400, '库存不存在');

        GoodsStoreStock::addStock($request->store_id, $goods->id, $stock->stock_num * -1);

        $goods = Goods::with(['stocks' => function ($query) use($request) {
            $query->where('store_id', $request->store_id);
        }])->find($goods->id);

        return new GoodsResource($goods);
    }

    public function getLogList(Goods $goods, Request $request)
    {
        $logs = GoodsStoreStockLog::latest()->where([
            'goods_id' => $goods->id,
            'store_id' => $request->store_id ?? 0,
        ])->paginate();

        return GoodsStockLogResource::collection($logs);
    }
}
